package project02.cool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Helper {

    private static final Helper instance = new Helper();

    private Helper() {}

    public static Helper getInstance() {
        return instance;
    }

    /**
     * Calendar → String
     */
    public String calToString(Calendar cal) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(cal.getTime());
    }

    /**
     * Calendar → YYMMDD
     */
    public String getYYMMDD(Calendar cal) {
        return getYear(cal) + getMonth(cal) + getDay(cal);
    }

    /**
     * Calendar → YYMMDD
     */
    public String getYYMMDDHH(Calendar cal) {
        return getYear(cal) + getMonth(cal) + getDay(cal) + getHour(cal);
    }

    /**
     * Calendar → Year
     */
    public String getYear(Calendar cal) {
        return String.format("%04d", cal.get(Calendar.YEAR));
    }

    /**
     * Date → Year
     */
    public String getYear(Date date) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return String.format("%04d", cal.get(Calendar.YEAR));
    }

    /**
     * Calendar → Month
     */
    public String getMonth(Calendar cal) {
        return String.format("%02d", (cal.get(Calendar.MONTH) + 1));
    }

    /**
     * Date → Month
     */
    public String getMonth(Date date) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return String.format("%02d", (cal.get(Calendar.MONTH) + 1));
    }

    /**
     * Calendar → Day
     */
    public String getDay(Calendar cal) {
        return String.format("%02d", cal.get(Calendar.DATE));
    }

    /**
     * Date → Day
     */
    public String getDay(Date date) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return String.format("%02d", cal.get(Calendar.DATE));
    }

    /**
     * Calendar → Hour
     */
    public String getHour(Calendar cal) {
        return String.format("%02d", cal.get(Calendar.HOUR_OF_DAY));
    }

    /**
     * Date → Hour
     */
    public String getHour(Date date) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return String.format("%02d", cal.get(Calendar.HOUR_OF_DAY));
    }

    public String getMinuts(Date date) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return String.format("%02d", cal.get(Calendar.MINUTE));
    }

    /**
     * 日本標準時に修正
     */
    public Calendar getGMT8Cal(Calendar cal) {

        Calendar result = Calendar.getInstance();
        result.setTimeInMillis(cal.getTimeInMillis() + 32400000);

        return result;
    }

}
